<?php
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/photo/redimmensionneur.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/photo/PhotoUtils.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/miniaturephoto/MiniaturePhotoEntite.php';

class MiniaturePhotoService {
		
		private function MiniaturePhotoService() {
			//évite la construction
		}
		
		public static function getNomRepertoireMiniature() {
			return "thumb";
		}
		
		public static function getMiniature($photo) {
			$nomCompletPhoto = PhotoUtils::getNomCompletPhoto($photo);
			//regarde si la miniature existe
			if(!MiniaturePhotoService::isMiniatureExisteFromNomCompletPhoto($nomCompletPhoto)) {
				//crée la miniature si besoin
				MiniaturePhotoService::createMiniature($nomCompletPhoto);
			}
			$miniaturePhotoEntite = new MiniaturePhotoEntite;
			$miniaturePhotoEntite->nomPhoto = $photo->nomFichier;
			$miniaturePhotoEntite->cheminPhotoOrigine = MiniaturePhotoService::getRepertoirePhotoFromNomCompletPhoto($nomCompletPhoto);
			$miniaturePhotoEntite->cheminMiniature = MiniaturePhotoService::getRepertoireMiniatureFromNomCompletPhoto($nomCompletPhoto);
			$miniaturePhotoEntite->nomCompletMiniature = $miniaturePhotoEntite->cheminMiniature."/".$photo->nomFichier;
			return $miniaturePhotoEntite;
		}
		
		public static function createMiniature($nomCompletPhoto) {
			$nomPhoto = basename($nomCompletPhoto);
			$repertoirePhoto = $_SERVER["DOCUMENT_ROOT"].dirname($nomCompletPhoto)."/";
			$repertoireMiniature = $_SERVER["DOCUMENT_ROOT"].MiniaturePhotoService::getRepertoireMiniatureFromNomCompletPhoto($nomCompletPhoto)."/";
			if (!is_dir($repertoireMiniature)) {
				//création du répertoire 
				@mkdir ($repertoireMiniature, 0777, true);
			}
			fctredimimage(80, 80, $repertoireMiniature, '', $repertoirePhoto, $nomPhoto, 100);  
		}
		
		public static function isMiniatureExisteFromNomCompletPhoto($nomCompletPhoto) {
			$nomCompletMiniature = $_SERVER["DOCUMENT_ROOT"].MiniaturePhotoService::getNomCompletMiniatureFromNomCompletPhoto($nomCompletPhoto);
			return file_exists($nomCompletMiniature);
		}
		
		public static function getNomCompletMiniatureFromNomCompletPhoto($nomCompletPhoto) {
			$repertoireMiniature = MiniaturePhotoService::getRepertoireMiniatureFromNomCompletPhoto($nomCompletPhoto);
			return $repertoireMiniature."/".basename($nomCompletPhoto);
		}
		public static function getRepertoireMiniatureFromNomCompletPhoto($nomCompletPhoto) {
			$nomPhoto = basename($nomCompletPhoto);
			$repertoirePhoto = dirname($nomCompletPhoto);
			return $repertoirePhoto."/".MiniaturePhotoService::getNomRepertoireMiniature();
		}
		
		public static function getNomCompletPhotoFromNomCompletMiniature($nomCompletMiniature) {
			$repertoireMiniature = dirname($nomCompletMiniature);
			$repertoirePhoto = dirname($repertoireMiniature);
			return $repertoirePhoto."/".basename($nomCompletMiniature);
		}
		
		public static function getRepertoirePhotoFromNomCompletPhoto($nomCompletPhoto) {
			return dirname($nomCompletPhoto);
		}
}
?>